<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/8/28
 * Time: 00:04
 */

namespace App\Http\Controllers\Api\v1\Everyday;

use App\Tools\HoroscopeData;

use App\Http\Controllers\Controller;

class LuckythingController extends Controller
{
    // 行运数据
    public $transitsData = [];
    // 三限数据
    public $thirdprogressedData = [];
    // 天象数据
    public $nowData = [];
    // 本命数据
    public $natalData = [];
    public $userinfo = [];
    public $now_time = 0;

    // 五行得分  金 水 木 火 土
    public function init()
    {
//        $result = [];
        $result = $this->getColor();
        $result[] = $this->getSign();
//        $result[] = $this->getBearing();
//        $result[] = $this->getNum();
        return $result;
    }

    /**
     * 幸运颜色，幸运方位，幸运数字
     * @return array
     */
    public function getColor()
    {
        $utime = strtotime($this->userinfo['year'] . '-' . $this->userinfo['month'] . '-' . $this->userinfo['day'] . ' ' . $this->userinfo['hour'] . ':' . $this->userinfo['minute']);

        if ($this->userinfo['is_summer'])
        {
            $utime -= 3600;
        }

        $utime += (8 - $this->userinfo['timezone']) * 3600;

        $bazi1 = $this->getBazi($this->now_time, $this->nowData['planets'][0]['deg']);
        $bazi2 = $this->getBazi($utime, $this->natalData['planets'][0]['deg'], true);

        $value1 = $value2 = $value3 = null;
        /**
         * 幸运补充
         * 检测当日日支与原命局关系
         * 有合
         *  无冲
         *      有穿（或刑） 取当日天干之偏才
         *      无穿 取当日天干
         *  有冲
         *      有穿 正常逻辑
         *      无穿 正常逻辑
         * 无合
         *  无冲
         *      有穿 取当日天干之食神
         *      无穿 正常逻辑
         *  有冲
         *      有穿 取当日天干相合之干
         *      无穿 取当日天干之伤官
         */
        $tiangan = null;
        $Horoscope = HoroscopeData::getInstance();
        // 是否有六合
        $is_liuhe = (!$Horoscope->is_liuhe($bazi1[6], $bazi2[2])
            && !$Horoscope->is_liuhe($bazi1[6], $bazi2[4])
            && !$Horoscope->is_liuhe($bazi1[6], $bazi2[6])
            && !$Horoscope->is_liuhe($bazi1[6], $bazi2[8])) ? false : true;
        // 是否有六冲
        $is_liuchong = (!$Horoscope->is_liuchong($bazi1[6], $bazi2[2])
            && !$Horoscope->is_liuchong($bazi1[6], $bazi2[4])
            && !$Horoscope->is_liuchong($bazi1[6], $bazi2[6])
            && !$Horoscope->is_liuchong($bazi1[6], $bazi2[8])) ? false : true;
        // 是否有六害
        $is_liuhai = (!$Horoscope->is_liuhai($bazi1[6], $bazi2[2])
            && !$Horoscope->is_liuhai($bazi1[6], $bazi2[4])
            && !$Horoscope->is_liuhai($bazi1[6], $bazi2[6])
            && !$Horoscope->is_liuhai($bazi1[6], $bazi2[8])) ? false : true;
        // 是否有刑
        $is_xing = (!$Horoscope->is_xing($bazi1[6], $bazi2[2])
            && !$Horoscope->is_xing($bazi1[6], $bazi2[4])
            && !$Horoscope->is_xing($bazi1[6], $bazi2[6])
            && !$Horoscope->is_xing($bazi1[6], $bazi2[8])) ? false : true;

        $shishenParam = $Horoscope->getShishenParam();
        $shishenTemp = null;
        // 有合
        if ($is_liuhe === true)
        {
            // 无冲
            if ($is_liuchong === false)
            {
                if ($is_liuhai || $is_xing)
                {
                    // 取当日天干之偏才
                    $shishenTemp = '偏财';
                } else
                {
                    // 取当日天干
                    $shishenTemp = '比肩';
                }
            }
        } else
        {
            // 无冲
            if ($is_liuchong === false)
            {
                if ($is_liuhai)
                {
                    // 取当日天干之食神
                    $shishenTemp = '食神';
                }
            } else
            {
                if ($is_liuhai)
                {
                    // 取当日天干相合之干
                    foreach ($shishenParam as $g => $sss)
                    {
                        if ($Horoscope->is_wuhe($g, $bazi1[5]))
                        {
                            $tiangan = $g;
                        }
                    }
                } else
                {
                    // 取当日天干之伤官
                    $shishenTemp = '伤官';
                }
            }
        }
        if ($shishenTemp !== null)
        {
            foreach ($shishenParam as $g1 => $ss)
            {
                foreach ($ss as $g2 => $s)
                {
                    if ($g2 === $bazi1[5] && $shishenTemp === $s)
                    {
                        $tiangan = $g1;
                    }
                }
            }
        }
        // 若幸运补充条件满足，则
        if ($tiangan !== null)
        {
            // 天干对应颜色
            //  每个阳干对应其五行颜色1号
            //  每个阴干对应其五行颜色2号
            $tianganColor = ['甲' => 'wood_one', '乙' => 'wood_two', '丙' => 'fire_one', '丁' => 'fire_two', '戊' => 'soil_one', '己' => 'soil_two', '庚' => 'gold_one', '辛' => 'gold_two', '壬' => 'water_one', '癸' => 'water_two'];
            $value1 = $tianganColor[$tiangan];
            // 天干对应数字
            //  1234567899
            $tianganNum = ['甲' => 'one', '乙' => 'two', '丙' => 'three', '丁' => 'four', '戊' => 'five', '己' => 'six', '庚' => 'seven', '辛' => 'eight', '壬' => 'nine', '癸' => 'nine'];
             $value3 = $tianganNum[$tiangan];
            // 天干对应方位
            //  甲东
            //  乙东南
            //  丙南
            //  丁西南
            //  戊东北
            //  己西南
            //  庚西
            //  辛西北
            //  壬北
            //  癸东北
            $tianganBearing = ['甲' => 'east', '乙' => 'southeast', '丙' => 'south', '丁' => 'southwest', '戊' => 'northeast', '己' => 'southwest', '庚' => 'west', '辛' => 'northwest', '壬' => 'north', '癸' => 'northeast'];
            $value2 = $tianganBearing[$tiangan];
        }

        if ($value1 === null || $value2 === null || $value3 === null)
        {
            $arr = [
                2 => [
                    1 => 'water_one',
                    2 => 'water_two',
                    3 => 'water_three',
                ],
                3 => [
                    1 => 'gold_one',
                    2 => 'gold_two',
                    3 => 'gold_three',
                ],
                4 => [
                    1 => 'fire_one',
                    2 => 'fire_two',
                    3 => 'fire_three',
                ],
                5 => [
                    1 => 'wood_one',
                    2 => 'wood_two',
                    3 => 'wood_three',
                ],
                6 => [
                    1 => 'soil_one',
                    2 => 'soil_two',
                    3 => 'soil_three',
                ],
            ];
            //方向对应五行
            //  东
            //      木1
            //  东南
            //      木2
            //  南
            //      火
            //  西南
            //      土1
            //  西
            //      金1
            //  西北
            //      金2
            //  北
            //      水
            //  东北
            //      土2
            $arr2 = [
                2 => [
                    1 => 'north',
                ],
                3 => [
                    1 => 'west',
                    2 => 'northwest',
                ],
                4 => [
                    1 => 'south',
                ],
                5 => [
                    1 => 'east',
                    2 => 'southeast',
                ],
                6 => [
                    1 => 'southwest',
                    2 => 'northeast',
                ],
            ];
            //数字对应五行
            //  1水
            //      水1号
            //  2土
            //      土1号
            //  3木
            //      1号
            //  4木
            //      2
            //  5土
            //      2
            //  6金
            //      1
            //  7火
            //      1
            //  8金
            //      2
            //  9火
            //      2
            $arr3 = [
                2 => [
                    1 => 'one',
                ],
                3 => [
                    1 => 'six',
                    2 => 'eight',
                ],
                4 => [
                    1 => 'seven',
                    2 => 'nine',
                ],
                5 => [
                    1 => 'three',
                    2 => 'four',
                ],
                6 => [
                    1 => 'two',
                    2 => 'five',
                ],
            ];
            $score1 = [
                '金' => 0,
                '水' => 0,
                '木' => 0,
                '火' => 0,
                '土' => 0
            ];
            $score2 = [
                '金' => 0,
                '水' => 0,
                '木' => 0,
                '火' => 0,
                '土' => 0
            ];
            // 天象五行得分  计算流月流日
//        $this->getTianGanScore($score1,$bazi1[1],$bazi1[2]);
            $this->getTianGanScore($score1, $bazi1[3], $bazi1[4]);
            $this->getTianGanScore($score1, $bazi1[5], $bazi1[6]);
//        $this->getDizhiScore($score1,$bazi1[2]);
            $this->getDizhiScore($score1, $bazi1[4], true);
            $this->getDizhiScore($score1, $bazi1[6]);
            $this->getShengkeScore($score1, $bazi1[5]);

            $this->getTianGanScore($score2, $bazi2[1], $bazi2[2]);
            $this->getTianGanScore($score2, $bazi2[3], $bazi2[4]);
            $this->getTianGanScore($score2, $bazi2[5], $bazi2[6]);
            $this->getTianGanScore($score2, $bazi2[7], $bazi2[8]);
            $this->getDizhiScore($score2, $bazi2[2]);
            $this->getDizhiScore($score2, $bazi2[4], true);
            $this->getDizhiScore($score2, $bazi2[6]);
            $this->getDizhiScore($score2, $bazi2[8]);
            $this->getShengkeScore($score2, $bazi2[5]);

            $score = [];

            foreach ($score1 as $k => $s) $score[$k] = $score1[$k] + $score2[$k];

            $sum = array_sum($score);

            $perc = [];
            foreach ($score as $k => $s)
            {
                $key = 2;
//            if($k == '水') $key = 2;
                if ($k == '金') $key = 3;
                if ($k == '火') $key = 4;
                if ($k == '木') $key = 5;
                if ($k == '土') $key = 6;
                $perc[$key] = intval(round($s / $sum * 100, 0));// 转换为整型百分比
            }
            $min = min($perc);

            if ($min == 0)
            {
                $count_value = array_count_values($perc);
                if ($count_value[$min] > 1)
                {
                    // 日干的五行
                    $rizhu_wuxing = config('horoscope.wuxing')[$bazi1[5]];
                    foreach ($perc as $wx => $v)
                    {
                        if ($v == $min)
                        {
                            switch ($rizhu_wuxing)
                            {
                                case '金':
                                    if ($wx == 6) $i = $wx;

                                    elseif ($wx == 2) $i = $wx;

                                    elseif ($wx == 3) $i = $wx;

                                    elseif ($wx == 5) $i = $wx;

                                    else $i = $wx;
                                    break;
                                case '水':
                                    if ($wx == 3) $i = $wx;

                                    elseif ($wx == 5) $i = $wx;

                                    elseif ($wx == 2) $i = $wx;

                                    elseif ($wx == 4) $i = $wx;

                                    else $i = $wx;
                                    break;
                                case '木':
                                    if ($wx == 2) $i = $wx;

                                    elseif ($wx == 4) $i = $wx;

                                    elseif ($wx == 5) $i = $wx;

                                    elseif ($wx == 6) $i = $wx;

                                    else $i = $wx;
                                    break;
                                case '火':
                                    if ($wx == 5) $i = $wx;

                                    elseif ($wx == 6) $i = $wx;

                                    elseif ($wx == 4) $i = $wx;

                                    elseif ($wx == 3) $i = $wx;

                                    else $i = $wx;
                                    break;
                                case '土':
                                    if ($wx == 4) $i = $wx;

                                    elseif ($wx == 3) $i = $wx;

                                    elseif ($wx == 6) $i = $wx;

                                    elseif ($wx == 2) $i = $wx;

                                    else $i = $wx;
                                    break;
                            }
                        }
                    }

                    $value1 = $arr[$i][3];
                    $value2 = $arr2[$i][1];
                    $value3 = $arr3[$i][1];
                } else
                {
                    $value1 = $arr[array_search($min, $perc)][3];
                    $value2 = $arr2[array_search($min, $perc)][1];
                    $value3 = $arr3[array_search($min, $perc)][1];
                }
            } elseif ($min < 10)
            {
                $value1 = $arr[array_search($min, $perc)][2];
                $value2 = isset($arr2[array_search($min, $perc)][2]) ? $arr2[array_search($min, $perc)][2] : $arr2[array_search($min, $perc)][1];
                $value3 = isset($arr3[array_search($min, $perc)][2]) ? $arr3[array_search($min, $perc)][2] : $arr3[array_search($min, $perc)][1];
            } else
            {
                if ($min < 15)
                {
                    $value2 = isset($arr2[array_search($min, $perc)][2]) ? $arr2[array_search($min, $perc)][2] : $arr2[array_search($min, $perc)][1];
                    $value3 = isset($arr3[array_search($min, $perc)][2]) ? $arr3[array_search($min, $perc)][2] : $arr3[array_search($min, $perc)][1];
                } else
                {
                    $value2 = $arr2[array_search($min, $perc)][1];
                    $value3 = $arr3[array_search($min, $perc)][1];
                }
                $value1 = $arr[array_search($min, $perc)][1];
            }
        }

        $fortuneConfig = config('fortune');
        // 幸运色
        $icon_url = $fortuneConfig[$value1];
        // 幸运方位
        $icon_url2 = $fortuneConfig[$value2];
        // 幸运数字
        $icon_url3 = $fortuneConfig[$value3];

        $result = [
            ['name' => '幸运色', 'icon_url' => $icon_url],
            ['name' => '幸运方位', 'icon_url' => $icon_url2],
            ['name' => '幸运数字', 'icon_url' => $icon_url3]
        ];

        return $result;
    }

    /**
     * 获取幸运方位
     * @return array
     */
    public function getBearing()
    {
        $arr = [
            0 => 'northeast', 1 => 'east', 2 => 'southeast', 3 => 'south',
            4 => 'southwest', 5 => 'west', 6 => 'northwest', 7 => 'north'
        ];

        $user_year = $this->userinfo['year'];
        $user_month = $this->userinfo['month'];
        $user_day = $this->userinfo['day'];

        $now_year = date('Y', $this->now_time);
        $now_month = date('m', $this->now_time);
        $now_day = date('d', $this->now_time);

        $num = ($user_year + $user_month + $user_day + $now_year + $now_month + $now_day) % 8;

        $icon_url = config('fortune.' . $arr[$num]);

        $result = ['name' => '幸运方位', 'icon_url' => $icon_url];

        return $result;
    }

    /**
     * 获取幸运星座
     * @return array
     */
    public function getSign()
    {
        $arr = [
            1 => 'aries', 2 => 'taurus', 3 => 'gemini', 4 => 'cancer',
            5 => 'leo', 6 => 'virgo', 7 => 'libra', 8 => 'scorpio',
            9 => 'sagittarius', 10 => 'capricornus', 11 => 'aquarius', 12 => 'pisces'
        ];
        $sign = 1;

        $tr_planet = $this->transitsData[1]['planets'];
        $na_planet = $this->natalData['planets'];
        $tr_phase_num = [60 => 0, 90 => 0, 120 => 0, 180 => 0];
        $na_phase_num = [60 => 0, 90 => 0, 120 => 0, 180 => 0];
        $tr_phase = [];
        $na_phase = [];
        $sign_arr = [1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => [], 7 => [], 8 => [], 9 => [], 10 => [], 11 => [], 12 => []];
        // 星体落入星座
        for ($i = 0; $i < 10; $i++)
        {
            $sign_arr[$tr_planet[$i]['in_sign_id']][] = $i;

            if ($i < 5)
            {
                if (!empty($tr_planet[$i]['phase']))
                {
                    foreach ($tr_planet[$i]['phase'] as $phase)
                    {
                        if ($phase['phase_e'] != 0 && isset($tr_phase_num[$phase['phase_e']]))
                        {
                            $tr_phase_num[$phase['phase_e']]++;// 计算行运日月水金火的各相位数量
                            $tr_phase[$phase['phase_e']][] = $phase['id'];// 成相的本命星体
                        }
                    }
                }
                if (!empty($na_planet[$i]['phase']))
                {
                    foreach ($na_planet[$i]['phase'] as $phase)
                    {
                        if ($phase['phase_e'] != 0 && isset($na_phase_num[$phase['phase_e']]))
                        {
                            $na_phase_num[$phase['phase_e']]++;// 计算本命日月水金火的各相位数量
                            $na_phase[$phase['phase_e']][] = $phase['id'];// 成相的本命星体
                        }
                    }
                }
            }
        }

        // 如当日行运有冲相位，取与冲相位内盘三合的星座
        //  优先顺时针
        //  如顺时针三合星座有天象星体，则取逆时针
        //  如都有星体，则取星体少的
        //  如星体个数相同，取顺时针
        //  如果冲相位不唯一，按行运行星优先 【月日水金火】
        if ($tr_phase_num[180] > 0)
        {
            $sign = $this->getSignId($tr_phase_num, $tr_phase, $sign_arr, 180);
        } elseif ($tr_phase_num[90] > 0)
        {
            $sign = $this->getSignId($tr_phase_num, $tr_phase, $sign_arr, 90);
        } elseif ($tr_phase_num[120] > 0)
        {
            $sign = $this->getSignId($tr_phase_num, $tr_phase, $sign_arr, 120);
        } elseif ($tr_phase_num[60] > 0)
        {
            $sign = $this->getSignId($tr_phase_num, $tr_phase, $sign_arr, 60);
        } else
        {
            if ($na_phase_num[180] > 0)
            {
                $sign = $this->getSignId($na_phase_num, $na_phase, $sign_arr, 180);
            } elseif ($na_phase_num[90] > 0)
            {
                $sign = $this->getSignId($na_phase_num, $na_phase, $sign_arr, 90);
            } elseif ($na_phase_num[120] > 0)
            {
                $sign = $this->getSignId($na_phase_num, $na_phase, $sign_arr, 120);
            } elseif ($na_phase_num[60] > 0)
            {
                $sign = $this->getSignId($na_phase_num, $na_phase, $sign_arr, 60);
            }
        }
        // 幸运星座具体名称
//        $name = mb_substr(explode('|',config('astrolog.sign_name')[$sign])[3],0,2,'utf-8');
        
        $icon_url = config('fortune.' . $arr[$sign]);

//        $result = ['name'=>'幸运'.$name,'icon_url'=>$icon_url];
        $result = ['name' => '幸运星座', 'icon_url' => $icon_url];

        return $result;
    }

    /**
     * 获取用户幸运数字
     * @return array
     */
    public function getNum()
    {
        $arr = [1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine',];

        $user_year = $this->userinfo['year'];
        $user_month = $this->userinfo['month'];
        $user_day = $this->userinfo['day'];

        $now_year = date('Y', $this->now_time);
        $now_month = date('m', $this->now_time);
        $now_day = date('d', $this->now_time);

        $num = ($user_year + $user_month + $user_day + $now_year + $now_month + $now_day) % 9;

        if ($num == 0) $num = 9;

        $icon_url = config('fortune.' . $arr[$num]);

        $result = ['name' => '幸运数字', 'icon_url' => $icon_url];

        return $result;
    }

    /**
     * 获取星座id
     * @param $tr_phase_num
     * @param $tr_phase
     * @param $sign_arr
     * @param int $deg
     * @return int
     */
    private function getSignId($tr_phase_num, $tr_phase, $sign_arr, $deg = 180)
    {
        $sign = 1;

        $na_planet = $this->natalData['planets'];

        $sequence = [1, 0, 2, 3, 4];// 相位不唯一时计算顺序【月日水金火】

        $change = 4;// 三合星座

        if ($tr_phase_num[$deg] == 1)
        {
            $tr_na_in_sign = $na_planet[$tr_phase[$deg][0]]['in_sign_id'];

            // 顺时针n合星座id
            $uninverse_sign = $tr_na_in_sign - $change;
            // 六分相特殊处理 TODO
            if ($deg == 60)
            {

            }


            if ($uninverse_sign <= 0) $uninverse_sign += 12;

            // 逆时针n合星座id
            $inverse_sign = $tr_na_in_sign + $change;

            if ($inverse_sign > 12) $inverse_sign -= 12;

            $uninverse_sign_count = count($sign_arr[$uninverse_sign]);

            $inverse_sign_count = count($sign_arr[$inverse_sign]);

            // 若顺逆星体数一致 取顺行
            if ($uninverse_sign_count == $inverse_sign_count)
            {
                $sign = $uninverse_sign;
            } // 都有星体 取少的
            elseif ($uninverse_sign_count > 0 && $inverse_sign_count > 0)
            {
                if ($uninverse_sign_count < $inverse_sign_count)
                {
                    $sign = $uninverse_sign;
                } else
                {
                    $sign = $inverse_sign;
                }
            } // 优先顺时针
            elseif ($uninverse_sign_count > 0)
            {
                $sign = $inverse_sign;
            } else
            {
                $sign = $uninverse_sign;
            }
        } // 有多颗成相
        else
        {
            foreach ($sequence as $i)
            {
                // 检测星体是否有冲相位
                if (!$sign && in_array($i, $tr_phase[$deg]))
                {
                    $tr_na_in_sign = $na_planet[$i]['in_sign_id'];

                    // 顺时针三合星座id
                    $uninverse_sign = $tr_na_in_sign - $change;

                    if ($uninverse_sign < 0) $uninverse_sign += 12;

                    // 逆时针三合星座id
                    $inverse_sign = $tr_na_in_sign + $change;

                    if ($inverse_sign > 12) $inverse_sign -= 12;

                    $uninverse_sign_count = count($sign_arr[$uninverse_sign]);

                    $inverse_sign_count = count($sign_arr[$inverse_sign]);

                    // 若顺逆星体数一致 取顺行
                    if ($uninverse_sign_count == $inverse_sign_count)
                    {
                        $sign = $uninverse_sign;
                    } // 都有星体 取少的
                    elseif ($uninverse_sign_count > 0 && $inverse_sign_count > 0)
                    {
                        if ($uninverse_sign_count < $inverse_sign_count)
                        {
                            $sign = $uninverse_sign;
                        } else
                        {
                            $sign = $inverse_sign;
                        }
                    } // 优先顺时针
                    elseif ($uninverse_sign_count > 0)
                    {
                        $sign = $inverse_sign;
                    } else
                    {
                        $sign = $uninverse_sign;
                    }

                    break;
                }
            }
        }
        return $sign;
    }

    /**
     * 获取天干五行得分
     * @param $score
     * @param null $gan
     * @param null $zhi
     */
    private function getTianGanScore(&$score, $gan = null, $zhi = null)
    {
        $base = 6;
        $config_wuxing = config('horoscope.wuxing');
        $config_shishen = config('horoscope.shishen');
        $config_canggan = config('horoscope.canggan');

        $wuxing = $config_wuxing[$gan];
        $status = false;
        $change = 0;
        foreach ($config_canggan[$zhi] as $k1 => $cg1)
        {
            // 本气40
            if ($k1 === 0)
            {
                // 坐支本气为相同五行成分
                if ($wuxing == $config_wuxing[$cg1])
                {
                    $status = true;
                    $change += 5;
                }
                // 坐支本气正印
                if ($config_shishen[$cg1][$gan] == '正印')
                {
                    $status = true;
                    $change += 3;
                } // 坐支本气偏印
                elseif ($config_shishen[$cg1][$gan] == '偏印')
                {
                    $status = true;
                    $change += 1;
                } // 坐支本气七杀
                elseif ($config_shishen[$cg1][$gan] == '七杀')
                {
                    $status = true;
                    $change += -4;
                } // 坐支本气正官
                elseif ($config_shishen[$cg1][$gan] == '正官')
                {
                    $status = true;
                    $change += -2;
                } // 坐支本气正财
                elseif ($config_shishen[$cg1][$gan] == '正财')
                {
                    $status = true;
                    $change += -1;
                } // 坐支本气偏财
                elseif ($config_shishen[$cg1][$gan] == '偏财')
                {
                    $status = true;
                    $change += -1;
                }
            } else
            {
                // 坐支非本气为相同五行成分
                if ($wuxing == $config_wuxing[$cg1])
                {
                    $status = true;
                    $change += 3;
                }
            }
        }
        if ($status == true) $change += $base;

        $score[$wuxing] += $change;
    }

    /**
     * 获取地支五行得分
     * @param $score
     * @param null $zhi
     * @param bool $is_m 是否为月令
     */
    private function getDizhiScore(&$score, $zhi = null, $is_m = false)
    {
        $metal = $wood = $water = $fire = $earth = 0;
        switch ($zhi)
        {
            case '子':
                $water += 10;
                break;
            case '丑':
                $earth += 6;
                $water += 2;
                $metal += 2;
                break;
            case '寅':
                $wood += 7;
                $fire += 2.5;
                $earth += 0.5;
                break;
            case '卯':
                $wood += 10;
                break;
            case '辰':
                $earth += 6;
                $wood += 2;
                $water += 2;
                break;
            case '巳':
                $fire += 7;
                $earth += 2.5;
                $metal += 0.5;
                break;
            case '午':
                $fire += 8;
                $earth += 4;
                break;
            case '未':
                $earth += 6;
                $wood += 2;
                $fire += 2;
                break;
            case '申':
                $metal += 7;
                $water += 2.5;
                $earth += 0.5;
                break;
            case '酉':
                $metal += 10;
                break;
            case '戌':
                $earth += 6;
                $fire += 2;
                $metal += 2;
                break;
            case '亥':
                $water += 7;
                $wood += 3;
                break;
        }

        if ($is_m)
        {
            $metal *= 2;
            $wood *= 2;
            $water *= 2;
            $fire *= 2;
            $earth *= 2;
        }
        $score['金'] += $metal;
        $score['木'] += $wood;
        $score['水'] += $water;
        $score['火'] += $fire;
        $score['土'] += $earth;
    }

    /**
     * 五行生克得分
     * @param $score
     * @param null $rizhu
     */
    private function getShengkeScore(&$score, $rizhu = null)
    {
        $config_wuxing = config('horoscope.wuxing');

        $wuxing = $config_wuxing[$rizhu];

        switch ($wuxing)
        {
            case '金':
                $score['金'] *= 1;// 同我
                $score['土'] *= 0.5;// 生我
                $score['火'] *= 1;// 克我
                $score['木'] *= 0.6;// 我克
                $score['水'] *= 0.3;// 我生
                break;
            case '水':
                $score['水'] *= 1;
                $score['金'] *= 0.5;
                $score['土'] *= 1;
                $score['火'] *= 0.6;
                $score['木'] *= 0.3;
                break;
            case '木':
                $score['木'] *= 1;
                $score['水'] *= 0.5;
                $score['金'] *= 1;
                $score['土'] *= 0.6;
                $score['火'] *= 0.3;
                break;
            case '火':
                $score['火'] *= 1;
                $score['木'] *= 0.5;
                $score['水'] *= 1;
                $score['金'] *= 0.6;
                $score['土'] *= 0.3;
                break;
            case '土':
                $score['土'] *= 1;
                $score['火'] *= 0.5;
                $score['木'] *= 1;
                $score['水'] *= 0.6;
                $score['金'] *= 0.3;
                break;
        }
    }

    /**
     * 获取八字
     * @param $time
     * @param int $sunDeg
     * @param bool $natal
     * @return array
     */
    private function getBazi($time, $sunDeg = 0, $natal = false)
    {
        // 十天干
        $Heavenly_Stems = config('horoscope.heavenly_stems');
        // 十二地支
        $Earthly_Branches = config('horoscope.earthly_branches');
        // 六十甲子
        $chinese_era = config('horoscope.chinese_era');

        $year = date('Y', $time) * 1;
        $month = date('m', $time) * 1;
        $day = date('d', $time) * 1;

//        $sunDeg = $this->nowData['planets'][0]['deg'];

        $year_stem_branche = ($year - 4) % 60 < 0 ? ($year - 4) % 60 + 60 : ($year - 4) % 60;

        if ($month < 3 && $sunDeg < 315)
        {
            $year_stem_branche--;

            if ($year_stem_branche < 0) $year_stem_branche = 59;
        }

        $bazi_str = $chinese_era[$year_stem_branche];
        // 每15度一个节气   总共二十四节气  月令地支
        $month_branche = ["卯", "辰", "辰", "巳", "巳", "午", "午", "未", "未", "申", "申", "酉", "酉", "戌", "戌", "亥", "亥", "子", "子", "丑", "丑", "寅", "寅", "卯"];

        $temp = ["寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥", "子", "丑"];// 十二个月

        // $temp2 = ["丙","戊","庚","壬","甲","丙","戊","庚","壬","甲"];
        $temp2 = [2, 4, 6, 8, 0, 2, 4, 6, 8, 0];

        $month_branche_num = intval(floor($sunDeg / 15));// 向下取整

        $month_branche_str = $month_branche[$month_branche_num];// 月令地支

        $year_stem = mb_substr($bazi_str, 0, 1, 'utf-8');

        $year_num = array_search($year_stem, $Heavenly_Stems);

        $start_num = $temp2[$year_num];

        $month_branche_num = array_search($month_branche_str, $temp);

        $newArr = array_merge($Heavenly_Stems, $Heavenly_Stems, $Heavenly_Stems);

        $s = [];

        for ($i = $start_num; $i < $start_num + 12; $i++) $s[] = $newArr[$i];

        $month_stem_branche = $s[$month_branche_num] . $month_branche_str;

        $time2 = 941990400;

        $time1 = strtotime(date('Y-m-d', $time));
        // 日辰初始使用24点
        $val = ($time1 - $time2) / 3600 / 24 % 60;

//        // 日辰初始使用23点
//        if($natal && date('H',$time) * 1 > 23)
//        {
//            $val++;
//            if($val == 60) $val = 0;
//        }

        $day_stem_branche = $val < 0 ? $val + 60 : $val;

        $bazi_str .= $month_stem_branche;

        $bazi_str .= $chinese_era[$day_stem_branche];

        $bazi = [
            1 => mb_substr($bazi_str, 0, 1, 'utf8'),
            2 => mb_substr($bazi_str, 1, 1, 'utf8'),
            3 => mb_substr($bazi_str, 2, 1, 'utf8'),
            4 => mb_substr($bazi_str, 3, 1, 'utf8'),
            5 => mb_substr($bazi_str, 4, 1, 'utf8'),
            6 => mb_substr($bazi_str, 5, 1, 'utf8'),
        ];

        if ($natal)
        {
            // 二十四小时，十二个时辰  时辰地支
            $hour_branche = ["子", "丑", "丑", "寅", "寅", "卯", "卯", "辰", "辰", "巳", "巳", "午", "午", "未", "未", "申", "申", "酉", "酉", "戌", "戌", "亥", "亥", "子"];

            // $temp2 = ["甲","丙","戊","庚","壬","甲","丙","戊","庚","壬"];
            $temp2 = [0, 2, 4, 6, 8, 0, 2, 4, 6, 8];

            $hour_branche_str = $hour_branche[date('H', $time) * 1];// 时辰地支


            $day_stem_branche = mb_substr($day_stem_branche, 0, 1, 'utf-8');

            $day_num = array_search($day_stem_branche, $Heavenly_Stems);

            $start_num = $temp2[$day_num];

            $hour_branche_num = array_search($hour_branche_str, $Earthly_Branches);

            $newArr = array_merge($Heavenly_Stems, $Heavenly_Stems, $Heavenly_Stems);

            $s = [];
            for ($i = $start_num; $i < $start_num + 12; $i++)
            {
                $s[] = $newArr[$i];
            }

            $bazi_str .= $s[$hour_branche_num] . $hour_branche_str;

            $bazi[7] = mb_substr($bazi_str, 6, 1, 'utf8');
            $bazi[8] = mb_substr($bazi_str, 7, 1, 'utf8');
        }

        return $bazi;
    }
}